package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.satu.quizcreator.quiz.Answer;
import com.example.satu.quizcreator.R;

import java.util.ArrayList;

import roboguice.fragment.RoboFragment;
import roboguice.inject.InjectView;

/**
 * Created by Satu on 2014-09-05.
 */
public class Multi_answer_fragment extends RoboFragment {

    @InjectView(R.id.good_answer_list)
    private ListView answerList;

    AnswerAdapter answerAdapter;

    @InjectView(R.id.button_add_bad_answer)
    ImageButton addAnswerButton;

    ArrayList<Answer> answers = null;

    public Multi_answer_fragment(ArrayList<Answer> answers) {
        this.answers = answers;
    }

    public Multi_answer_fragment(){

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.multi_answer_fragment,null);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        answerAdapter = new AnswerAdapter(getActivity(),R.layout.answer_widget_description,null);
        if(answers != null){
            for (Answer answer : answers){
                answerAdapter.add(answer);
            }
        }else
             answerAdapter.add(new Answer(getResources().getString(R.string.write_answer)));
        answerList.setAdapter(answerAdapter);

        addAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                answerAdapter.add(new Answer(getActivity().getResources().getString(R.string.write_answer)));
            }
        });
     }
    public AnswerAdapter getAnswerAdapter() {
        return answerAdapter;
    }
    public ArrayList<Answer> getAnswers(){
        ArrayList<Answer> answers = new ArrayList<Answer>();
        for (int i = 0; i < answerAdapter.getCount();i++){
            answers.add(answerAdapter.getItem(i));
        }
        return answers;
    }

}
